package top.lvkun.org.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import top.lvkun.basic.service.impl.BaseServiceImpl;
import top.lvkun.basic.utils.PageList;
import top.lvkun.org.domain.Department;
import top.lvkun.org.mapper.DepartmentMapper;
import top.lvkun.org.query.DepartmentQuery;
import top.lvkun.org.service.IDepartmentService;

import java.util.List;

@Service
public class DepartmentServiceImpl extends BaseServiceImpl<Department> implements IDepartmentService {

    @Autowired
    DepartmentMapper departmentMapper;

    @Override
    public void add(Department department) {
        departmentMapper.save(department);
        this.update(department);
    }

    @Override
    public void update(Department department) {
        System.out.println(department);
            if (department.getParent() == null || department.getParent().getId()== null) {
                department.setDirPath("/" + department.getId());
            } else {
                department.setDirPath(departmentMapper.loadById(department.getParent().getId()).getDirPath() + "/" + department.getId());
                /*if (department.getParent().getId()!= null) {
                    department.setDirPath(departmentMapper.loadById(department.getParent().getId()).getDirPath() + "/" + department.getId());
                }else {
                    department.setDirPath("/" + department.getId());
                }*/
            }
            departmentMapper.update(department);
    }

    @Override
    public List<Department> getDeptTree() {
        return departmentMapper.getDeptTree();
    }

}
